//
//  LYQHomeAPICell.m
//  ZCProject
//
//  Created by Zheng on 16/11/8.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQHomeAPICell.h"

@implementation LYQHomeAPICell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.pageView];
        [self.contentView addSubview:self.lineView];
        
        [self layoutSubviewsInContentView];
        
    }
    return self;
}

#pragma mark - layout subviews
- (void)layoutSubviewsInContentView {
    
    [_pageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.bottom.mas_equalTo(-kWScale(2.5));
    }];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self);
        make.top.equalTo(_pageView.mas_bottom);
    }];
    
}

#pragma mark - load subviews
- (UIScrollView *)pageView {
    if (!_pageView) {
        _pageView = [[UIScrollView alloc] init];
        _pageView.contentSize = CGSizeMake(kMS_Width*2, kWScale(180));
        _pageView.pagingEnabled = YES;
        _pageView.showsHorizontalScrollIndicator = NO;
        _pageView.bounces = NO;
        
        NSArray *titleNamesArr = @[@"地接社",@"机票",@"酒店",@"批发商",
                                   @"度假村",@"购物",@"游轮",@"酒店预订",
                                   @"租车",@"海岛",@"地接社",@"机票",
                                   @"酒店",@"批发商",@"度假村",@"购物",
                                   @"游轮",@"酒店预订",@"租车",@"海岛"];
        
        NSArray *imageNamesArr = @[@"dijieicon",@"jipiaoicon",@"jiudianicon",
                                   @"pifaicon",@"dujjiaicon", @"gouwuicon",
                                   @"youlunicon",@"jdydicon",@"zucheicon",
                                   @"haidaoicon",@"dijieicon",@"jipiaoicon",
                                   @"jiudianicon", @"pifaicon",@"dujjiaicon",
                                   @"gouwuicon",@"youlunicon",@"jdydicon",
                                   @"zucheicon",@"haidaoicon"];

        for (int i = 0; i < 20; i++) {
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [button setImage:[UIImage imageNamed:imageNamesArr[i]] forState:UIControlStateNormal];
            [button setTitle:titleNamesArr[i] forState:UIControlStateNormal];
            button.titleLabel.font = kFont(13);
            [button setTitleColor:kLYQBlack forState:UIControlStateNormal];
            [button setImageEdgeInsets:UIEdgeInsetsMake(-20, 12.5, 0, 0)];
            [button setTitleEdgeInsets:UIEdgeInsetsMake(60, -46, 0, 0)];
            
            [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

            CGRect bFrame;
            
            if (i < 10) {
                if (i%2 == 0) {
                    bFrame = CGRectMake(kWScale(7.5)+(kMS_Width-kWScale(15))/5*(i/2), 0, (kMS_Width-kWScale(15))/5, kWScale(90));
                } else {
                    bFrame = CGRectMake(kWScale(7.5)+(kMS_Width-kWScale(15))/5*(i/2), kWScale(90), (kMS_Width-kWScale(15))/5, kWScale(90));
                }
            } else {
                if (i%2 == 0) {
                    bFrame = CGRectMake(kMS_Width+kWScale(7.5)+(kMS_Width-kWScale(15))/5*((i-10)/2), 0, (kMS_Width-kWScale(15))/5, kWScale(90));
                } else {
                    bFrame = CGRectMake(kMS_Width+kWScale(7.5)+(kMS_Width-kWScale(15))/5*((i-10)/2), kWScale(90), (kMS_Width-kWScale(15))/5, kWScale(90));
                }
            }
            
            button.frame = bFrame;
            
            [_pageView addSubview:button];
        }
    }
    return _pageView;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kHexadecimal(@"e2e2e2");
        _lineView.alpha = 0.5;
    }
    return _lineView;
}

#pragma mark - 点击事件
- (void)btnAction:(UIButton *)sender {
    NSLog(@"title == %@",sender.titleLabel.text);
}


@end
